<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增教材</title>
    <link rel="stylesheet" href="/element-ui/index.css">
    <link rel="stylesheet" href="/element-ui/fonts/element-icons.css">
    <script src="/element-ui/vue.js"></script>
    <script src="/element-ui/index.js"></script>
    <script src="/element-ui/axios.min.js"></script>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '\\5FAE\\8F6F\\96C5\\9ED1', Arial, sans-serif;
            background-color: #f5f6fa;
        }
        #app {
            padding: 20px;
            background-color: #f5f6fa;
        }
        .page-title-bar {
            font-size: 26px;
            font-weight: bold;
            color: #333;
            background: #fff;
            padding: 24px 20px 12px 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
            letter-spacing: 2px;
            text-align: center;
            animation: fadeInDown 0.8s ease-out;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            border-radius: 8px;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .el-main {
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .el-form-item {
            margin-bottom: 22px;
        }
        .el-input__inner,
        .el-select .el-input__inner,
        .el-date-editor .el-input__inner {
            transition: all 0.3s ease;
        }
        .el-input__inner:focus,
        .el-input__inner:hover,
        .el-select .el-input__inner:focus,
        .el-select .el-input__inner:hover,
        .el-date-editor .el-input__inner:focus,
        .el-date-editor .el-input__inner:hover {
            border-color: #409EFF;
            box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
        .el-button {
            transition: all 0.3s ease;
        }
        .el-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
        .el-button--primary:hover {
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
    </style>
</head>
<body>
    <div class="page-title-bar">新增教材</div>
    <div id="app">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" ref="form" label-width="120px">
                    <el-form-item label="ISBN" prop="isbn">
                        <el-input v-model="form.isbn"></el-input>
                    </el-form-item>
                    <el-form-item label="教材名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="作者" prop="author">
                        <el-input v-model="form.author"></el-input>
                    </el-form-item>
                    <el-form-item label="出版社" prop="publisherId">
                        <el-select v-model="form.publisherId" placeholder="请选择出版社">
                            <el-option
                                v-for="publisher in publishers"
                                :key="publisher.id"
                                :label="publisher.name"
                                :value="publisher.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="价格" prop="price">
                        <el-input-number v-model="form.price" :precision="2" :step="0.1"></el-input-number>
                    </el-form-item>
                    <el-form-item label="出版日期" prop="publishDate">
                        <el-date-picker
                            v-model="form.publishDate"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="版次" prop="edition">
                        <el-input v-model="form.edition"></el-input>
                    </el-form-item>
                    <el-form-item label="描述" prop="description">
                        <el-input type="textarea" v-model="form.description"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('form')">提交</el-button>
                        <el-button @click="goBack">返回</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    form: {
                        isbn: '',
                        name: '',
                        author: '',
                        publisherId: '',
                        price: 0,
                        publishDate: '',
                        edition: '',
                        description: ''
                    },
                    publishers: [],
                    rules: {
                        isbn: [
                            { required: true, message: '请输入ISBN', trigger: 'blur' }
                        ],
                        name: [
                            { required: true, message: '请输入教材名称', trigger: 'blur' }
                        ],
                        author: [
                            { required: true, message: '请输入作者', trigger: 'blur' }
                        ],
                        publisherId: [
                            { required: true, message: '请选择出版社', trigger: 'change' }
                        ],
                        price: [
                            { required: true, message: '请输入价格', trigger: 'blur' }
                        ]
                    }
                }
            },
            created() {
                this.fetchPublishers();
            },
            methods: {
                fetchPublishers() {
                    axios.get('/publisher/all')
                        .then(response => {
                            this.publishers = response.data;
                        })
                        .catch(error => {
                            console.error('Error fetching publishers:', error);
                            this.$message.error('获取出版社列表失败');
                        });
                },
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            axios.post('/book/add', this.form)
                                .then(response => {
                                    if (response.data === 'success') {
                                        this.$message.success('添加成功');
                                        this.goBack();
                                    } else {
                                        this.$message.error('添加失败');
                                    }
                                })
                                .catch(error => {
                                    console.error('Error adding book:', error);
                                    this.$message.error('添加失败');
                                });
                        } else {
                            return false;
                        }
                    });
                },
                goBack() {
                    window.location.href = '/book/list';
                }
            }
        });
    </script>
</body>
</html> 